package com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.common.utils.ZhjdIotUtils;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.entity.AsphaltPenetrometerEntity;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.entity.AsphaltPenetrometerResEntity;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.entity.TaskEntity;
import com.qzsoft.zhjd.laboratory.modules.asphaltPenetrometer.service.AsphaltPenetrometerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * 沥青针入度仪
 *
 * @author sx
 * @email
 * @date 2023-07-11 17:18:05
 */
@RestController
@RequestMapping("laboratory/asphaltPenetrometer")
public class AsphaltPenetrometerController {

    @Autowired
    private AsphaltPenetrometerService asphaltPenetrometerService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("laboratory:asphaltPenetrometer:page")
    public Result page(@RequestParam Map<String, Object> params){
        IPage page = asphaltPenetrometerService.queryPage(params);
        return ResultUtils.ok(page);
    }


    /**
     * 解析北京中科建仪电子科技有限公司数据  -- 沥青软化点仪
     * @param deviceId
     * @param req
     * @return
     */
    @PostMapping("/parsingDataSoftPointerOfBjzkjydz/{deviceId}")
    public Result parsingDataSoftPointerOfBjzkjydz(@PathVariable("deviceId") String deviceId, HttpServletRequest req){
        asphaltPenetrometerService.parsingDataSoftPointerOfBjzkjydz(ZhjdIotUtils.dataParsing(req),deviceId);
        return ResultUtils.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/find/{id}")
    @RequiresPermissions("laboratory:asphaltPenetrometer:find")
    public Result find(@PathVariable("id") String id){
		AsphaltPenetrometerEntity asphaltPenetrometer = asphaltPenetrometerService.getInfo(id);
        return ResultUtils.ok(asphaltPenetrometer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("laboratory:asphaltPenetrometer:save")
    public Result save(@RequestBody AsphaltPenetrometerEntity asphaltPenetrometer){
		asphaltPenetrometerService.saveData(asphaltPenetrometer);
        return ResultUtils.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("laboratory:asphaltPenetrometer:update")
    public Result update(@RequestBody AsphaltPenetrometerEntity asphaltPenetrometer){
        asphaltPenetrometerService.updateData(asphaltPenetrometer);
        return ResultUtils.ok();
    }

    /**
     * 解析北京中科建仪电子科技有限公司数据  -- 针入度
     * @param deviceId
     * @param req
     * @return
     */
    @PostMapping("/parsingDataOfBjzkjydz/{deviceId}")
    public Result parsingDataOfBjzkjydz(@PathVariable("deviceId") String deviceId, HttpServletRequest req){
        asphaltPenetrometerService.parsingDataOfBjzkjydz(ZhjdIotUtils.dataParsing(req),deviceId);
        return ResultUtils.ok();
    }

    /**
     * 删除单个
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("laboratory:asphaltPenetrometer:delete")
    public Result deleteOne(@PathVariable("id") String id){
        asphaltPenetrometerService.deleteOneData(id);
        return ResultUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("laboratory:asphaltPenetrometer:delete")
    public Result delete(@RequestBody String[] ids){
        asphaltPenetrometerService.deleteData(Arrays.asList(ids));
        return ResultUtils.ok();
    }

    /**
     * 解析数据
     * @param deviceId
     * @param req
     * @return
     */
    @PostMapping("/parsingData/{deviceId}")
    public Result parsingData(@PathVariable("deviceId") String deviceId, HttpServletRequest req){
        asphaltPenetrometerService.parsingData(ZhjdIotUtils.dataParsing(req),deviceId);
        return ResultUtils.ok();
    }

    /**
     * 重新解析数据
     * @param deviceId
     * @return
     */
    @PostMapping("/reParsingData/{deviceId}")
    public Result reParsingData(@PathVariable("deviceId") String deviceId){
        asphaltPenetrometerService.reParsingData(deviceId);
        return ResultUtils.ok();
    }

    /**
     * 解析数据济微二标沥青针入度
     * @param deviceId
     * @param req
     * @return
     */
    @PostMapping("/parsingDataTwob/{deviceId}")
    public Result parsingDataTwob(@PathVariable("deviceId") String deviceId, HttpServletRequest req){
        asphaltPenetrometerService.parsingDataTwob(ZhjdIotUtils.dataParsing(req),deviceId);
        return ResultUtils.ok();
    }

    /**
     * 解析数据济微一标沥青针入度
     * @param json
     * @param
     * @return
     */
    @PostMapping("/parsingDataOneb")
    public String parsingDataOneb(@RequestBody JSONObject json){
        JSONObject result = new JSONObject();
        String key = "";
        try {
            key = asphaltPenetrometerService.parsingDataOneb(json);
            result.put("code", 200);
            result.put("data", key);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("济微一标沥青针入度数据上传错误："+e);
            result.put("code", 300);
        }
        return result.toJSONString();
    }

    /**
     * 绑定材料
     */
    @RequestMapping("/changeSample")
    @RequiresPermissions("laboratory:asphaltPenetrometer:changeSample")
    public Result changeSample(@RequestBody AsphaltPenetrometerResEntity asphaltPenetrometerResEntity){
        asphaltPenetrometerService.changeSample(asphaltPenetrometerResEntity);
        return ResultUtils.ok();
    }

    /**
     * 生成委托编号
     */
    @RequestMapping("/taskCode")
    public Result getTaskCode(@RequestParam("deviceNo") String deviceNo){
        TaskEntity entity = asphaltPenetrometerService.getTaskCode(deviceNo);
        return ResultUtils.ok(entity);
    }

    /**
     * 接收申克路业仪器有限公司的数据，
     * 济南至宁津四标段针入度；
     */
    @PostMapping("/data/entry")
    public Result dataEntry(@RequestBody JSONObject json){
        asphaltPenetrometerService.dataEntry(json);
        return ResultUtils.ok();
    }

    /**
     * 解析数据上海昌吉一标沥青针入度
     * @param json
     * @param
     * @return
     */
    @PostMapping("/parsingDataDTU/{deviceId}")
    public String parsingDataDTU(@PathVariable("deviceId") String deviceId, @RequestBody String json){
        JSONObject result = new JSONObject();
        String key = "";
        try {
            key = asphaltPenetrometerService.parsingDataDTU(deviceId, json);
            result.put("code", 200);
            result.put("data", key);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("上海昌吉一标沥青针入度数据上传错误："+e);
            result.put("code", 300);
        }
        return result.toJSONString();
    }
}
